package com.thunder.soft.common.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;

import com.gyf.immersionbar.ImmersionBar;

public abstract class BaseFragment<VDB extends ViewDataBinding> extends Fragment {
    protected VDB binding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, layoutId(), container, false);
        return binding.getRoot();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    protected abstract void initData();

    @LayoutRes
    protected abstract int layoutId();

    /**
     * 设置标题栏
     *
     * @param view 标题栏 view
     */
    protected void setTitleBar(View view) {
        ImmersionBar.setTitleBar(this, view);
    }

    /**
     * 设置状态栏 默认沉浸式 ，状态栏背景透明，字体颜色黑色
     */
    protected void setImmersionBar() {
        ImmersionBar.with(this)
                .transparentStatusBar()
                .statusBarDarkFont(true)
                .setOnKeyboardListener(this::onKeyboardChange)
                .init();
    }

    /**
     * 软键盘改变时调用此方法
     *
     * @param isPopup        是否弹出
     * @param keyboardHeight 软键盘高度
     */
    protected void onKeyboardChange(boolean isPopup, int keyboardHeight) {

    }

    @Override
    public void onDestroy() {
        if (binding != null) {
            binding.unbind();
            binding = null;
        }
        super.onDestroy();
    }
}
